package com.shine.icis.util;

import java.util.ArrayList;
import java.util.List;

import javax.sql.RowSet;

import com.shine.icis.dto.EFDept;
import com.shine.icis.dto.EFUser;
import com.shine.proxy.IServiceAgent;
import com.shine.proxy.ServiceComponentFactory;
import com.shine.util.ComInput;
import com.shine.util.ComOutRS;
import com.shine.util.ComOutput;

public class EFRameInf {
	public static final String APP_ID = "25";//系统ID
	public static final String HEAD_DEP_CODE = "#000000001";//总部DEP_CODE
	
	public static final String FUNCCODE_COLLECT = "25010101";//数据采集中心
	public static final String FUNCCODE_GENDATA= "25010102";//报送数据生成
	public static final String FUNCCODE_RPT = "25010103";//报送数据调整
	public static final String FUNCCODE_CHECKDATA= "25010104";//报送数据核对
	public static final String FUNCCODE_GENFILE= "25010105";//报送文件生成
	public static final String FUNCCODE_APPROVAL = "25010201";//报送文件审核
	public static final String FUNCCODE_FILEDOWNLOAD = "25010202";//生成文件下载
	public static final String FUNCCODE_ACCRUAL = "25010301";//应收应付配置
	public static final String FUNCCODE_BASEINFO = "25010302";//基础信息管理
	public static final String FUNCCODE_SUBMITCFG = "25010401";//报送配置
	public static final String FUNCCODE_SUBMITFLOW = "25010402";//报送审核配置
	public static final String FUNCCODE_BUSICONF = "25010403";//结算业务配置
	
	/**
	 * 向EFRAME插入日志
	 */
	public static void saveLog(String logType, String appId, String userCode, String funcCode, String opDesc, String retCode, String retDesc) {
		try {
			IServiceAgent sa = ServiceComponentFactory.createServiceAgent("EICIS");
			ComInput comInput = new ComInput();
			comInput.set("LOG_TYPE", logType);//1：操作日志；2：系统日志
			comInput.set("APP_ID", appId);//可以session中取，但目前session取到为0
			comInput.set("OP_USER_CODE", userCode);
			comInput.set("_FUNC_CODE", funcCode);//菜单代码
			comInput.set("OP_DESC", opDesc);//操作说明
			comInput.set("RET_CODE", retCode);//0：成功；-1：失败
			comInput.set("RET_DESC", retDesc);//操作结果："操作成功","修改成功","删除成功"
			ComOutput comOutput = (ComOutput)sa.dynaCallEjb("esystem", "saveLog", comInput);
			if (comOutput.getRetCode() == -1) {
				throw new Exception(comOutput.getRetDesc());
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 向EFRAME插入系统日志
	 * @param userCode
	 * @param funcCode
	 * @param opDesc
	 * @param retCode
	 */
	public static void saveSysLog(String userCode, String funcCode, String opDesc, boolean success) {
		saveLog("2", APP_ID, userCode, funcCode, opDesc, success ? "0" : "-1", success ? "操作成功" : "操作失败");
	}
	
	/**
	 * 向EFRAME插入操作日志
	 * @param userCode
	 * @param funcCode
	 * @param opDesc
	 * @param retCode
	 */
	public static void saveOpLog(String userCode, String funcCode, String opDesc, boolean success) {
		saveLog("1", APP_ID, userCode, funcCode, opDesc, success ? "0" : "-1", success ? "操作成功" : "操作失败");
	}
	
	/**
	 * 取得总部的DEP_ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getHeadDepId() {
		String depId = null;
		try {
			IServiceAgent sa = ServiceComponentFactory.createServiceAgent("EICIS");
			
			ComInput comInput = new ComInput();
			comInput.set("DEP_CODE", HEAD_DEP_CODE);//总部的
			ComOutput comOutput = (ComOutput)sa.dynaCallEjb("esystem", "getDepInfobyCode", comInput);
			if (comOutput.getRetCode() == -1) {
				throw new Exception(comOutput.getRetDesc());
			}
			
			List<ComOutRS> corsList = (List<ComOutRS>)comOutput.getArray();
			if (corsList != null && !corsList.isEmpty()) {
				RowSet rs = corsList.get(0).getRs();
				if(rs.next()) {
					depId = rs.getString(1);
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return depId;
	}
	
	/**
	 * 向EFRAME取得部门列表
	 * @param depId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<EFDept> getDepts(String depId) {
		List<EFDept> depts = new ArrayList<EFDept>();
		try {
			IServiceAgent sa = ServiceComponentFactory.createServiceAgent("EICIS");
			ComInput comInput = new ComInput();
			comInput.set("DEP_ID", depId);
			comInput.set("DEP_STATUS", "0");
			
			ComOutput comOutput = (ComOutput)sa.dynaCallEjb("esystem", "getAllDepTree", comInput);
			if (comOutput.getRetCode() == -1) {
				throw new Exception(comOutput.getRetDesc());
			}
			for (ComOutRS cors : (List<ComOutRS>)comOutput.getArray()) {
				RowSet rs = cors.getRs();
				while(rs.next()) {
					EFDept dept = new EFDept();
					dept.setDepId(rs.getString(1));
					dept.setDepName(rs.getString(2));
					dept.setParentDepId(rs.getString(3));
//					System.out.println("\nDEP_ID:" + rs.getString(1) + "\nDEP_NAME:" 
//							+ rs.getString(2) + "\nPARENT_DEP_ID:" + rs.getString(3));
					depts.add(dept);
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return depts;
	}
	
	@SuppressWarnings("unchecked")
	public static List<EFUser> getUsers(String depId, String userCode, String userGroup) {
		List<EFUser> users = new ArrayList<EFUser>();
		try {
			IServiceAgent sa = ServiceComponentFactory.createServiceAgent("EICIS");
			ComInput comInput = new ComInput();
			comInput.set("DEP_ID", depId);
			comInput.set("OP_USER_GROUP", userGroup);
			comInput.set("OP_USER_CODE", userCode);
			
			ComOutput comOutput = (ComOutput)sa.dynaCallEjb("esystem", "getAllUserInfo", comInput);
			if (comOutput.getRetCode() == -1) {
				throw new Exception(comOutput.getRetDesc());
			}
			for (ComOutRS cors : (List<ComOutRS>)comOutput.getArray()) {
				RowSet rs = cors.getRs();
				while(rs.next()) {
					EFUser user = new EFUser();
					user.setUserCode(rs.getString(2));
					user.setUserName(rs.getString(3));
//					System.out.println("\nOPERATE:" + rs.getString(1) + "\nUSER_CODE:" 
//							+ rs.getString(2) + "\nUSER_NAME:" + rs.getString(3) + "\nTEL:" 
//							+ rs.getString(4) + "\nNOBILE:" + rs.getString(5) + "\nEMAIL:" 
//							+ rs.getString(6) + "\nUSER_GROUP:" + rs.getString(7) + "\nDE_NAME:" 
//							+ rs.getString(8) + "\nUSER_STATUS:" + rs.getString(9) + "\nNOTE:" 
//							+ rs.getString(10) + "\nIS_UPDATE_PWD:" + rs.getString(11) + "\nIS_ALWAYS_VALID:" + 
//							rs.getString(12) + "\nREPIOD_OF_VALIDITY:" + rs.getString(13));
					users.add(user);
				}
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		return users;
	}
}
